package com.woniu.cq.mallorder.config;

import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.nio.charset.Charset;

/**
 * @author Daniel
 * @version 1.0.0
 * @ClassName RedisConfig
 * @Description TODO
 * @createTime 2020年09月16日 16:15:00
 */
public class RedisConfig {
    @Bean("redisTemplateObject")
    public RedisTemplate<String, Object> createRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> rt = new RedisTemplate<>();
        //设置链接
        rt.setConnectionFactory(redisConnectionFactory);
        //String序列化方式
        StringRedisSerializer srs = new StringRedisSerializer(Charset.forName("UTF-8"));
        rt.setKeySerializer(srs);
        rt.setHashKeySerializer(srs);
        //Object序列化方式
        Jackson2JsonRedisSerializer<Object> jrs = new Jackson2JsonRedisSerializer<>(Object.class);
        rt.setValueSerializer(jrs);
        rt.setHashValueSerializer(jrs);
        return rt;
    }
}
